/*
 用户设置
 * */
define([
        '../../common/service',
        '../../model/countryList',
        '../../model/timezoneList',
        'jquery-html5Validate'
    ],
    function(
        http,
        countryList,
        timezoneList
    ) {
        var Profile = {
            init: function(query) {
                // 初始化国家和时区的下拉框
                this.initDropdownList();
                // 查询当前用户的信息，填充输入框或者选定下拉框对应的值
                this.initData();
                this.bindSubmitEvent();
            },
            initDropdownList: function() {
                var _this = this;
                //国家
                var countryOptions = [{id:0,text:I18N.getText('profile','selectCountry')}];
                countryList = _.sortBy(countryList, function(item) {
                    return item.name.toLowerCase();
                });
                $.each(countryList, function(i, n) {
                    countryOptions.push({id:n.country_code,text: n.name});
                });
                _this.countryOptions = countryOptions;
                var myForm = $('#profileForm');
                $('[name="country"]', myForm).drop({
                    data: countryOptions,
                    name: "country"//为了方便serialize 建议设置name 属性
                });
                //时区
                var timezoneOptions = [{id:0,text:I18N.getText('profile','selectTimezone')}];
                $.each(timezoneList, function(i, n) {
                    timezoneOptions.push({id: n.key,text: n.name});
                });
                _this.timezoneOptions = timezoneOptions;
                $('[name="timezone"]', myForm).drop({
                    data: timezoneOptions,
                    name: "timezone"//为了方便serialize 建议设置name 属性
                });
                //语言
                var languageOptions = [{id:'zh-cn',text:'汉语(简)'}, {id:'en-us',text:'English(us)'}];
                $('[name="language"]', myForm).drop({
                    data: languageOptions,
                    name: "language"//为了方便serialize 建议设置name 属性
                })
            },
            initData: function() {
                    var _this = this;
                    var myForm = $('#profileForm');
                    Kit.loading.show(myForm);
                    http.work('/user/current', 'POST').then(function(res) {
                        var data = res.data;
                        if (res.flag == 'success') {
                            $('input[name="nickname"]', myForm).val(data.nickname || '');
                            $('[name="country"]', myForm).drop("val", Kit.findBykey( _this.countryOptions, data.country, "id") || _this.countryOptions[0]);
                            $('[name="timezone"]', myForm).drop("val", Kit.findBykey( _this.timezoneOptions, data.timezoneId, "id") || _this.timezoneOptions[0]);
                            $('input[name="language"]', myForm).val(data.language=='zh-cn'?'汉语(简)':'English(us)').attr('data-val', data.language || 'zh-cn');
                        } else {
                            Kit.alert(res.msg);
                        }
                        Kit.loading.hide(myForm);
                    });
                }
                /*
                 @绑定提交事件
                 含表单验证
                 */
                ,
            bindSubmitEvent: function() {
                var _this = this;
                var myForm = $('#profileForm');
                var submitForm = myForm.find('.submitForm');
                submitForm.unbind('click');
                submitForm.click(function() {
                    var nickname    = $('input[name="nickname"]', myForm),
                        country     = $('div[name="country"]', myForm),
                        timezone    = $('div[name="timezone"]', myForm),
                        language    = $('input[name="language"]', myForm);
                    var val_nickname = nickname.val().trim(),
                        val_country = country.data("data"),
                        val_timezone =  timezone.data("data"),
                        val_language = language.attr('data-val');
                    if ('' == val_nickname) {
                        Kit.alert_info(I18N.getText('user', 'enternickname'))
                        nickname.focus();
                        return false;
                    }
                    if (val_country.id  == 0) {
                        Kit.alert_info(I18N.getText('user', 'selectCountry'))
                        country.focus();
                        return false;
                    }
                    if (val_timezone.id  == 0) {
                        Kit.alert_info(I18N.getText('user', 'selectTimezone'))
                        timezone.focus();
                        return false;
                    }
                    var param = {
                        nickname: val_nickname,
                        country: val_country.id,
                        timezoneId: val_timezone.id,
                        timezoneName: val_timezone.text,
                        language: val_language
                    };
                    var loadingArea = $('#profileForm');
                    Kit.loading.show(loadingArea);
                    http.work('/user/edit', param, 'POST').then(function(res) {
                        var data = res.data;
                        if (res.flag == 'success') {
                            var rm = localStorage.getItem('rememberMe');
                            var reportTitle = window.localStorage.getItem('report-list-22');
                            localStorage.clear()
                            sessionStorage.clear();
                            window.localStorage.setItem('language', val_language);
                            window.localStorage.setItem('rememberMe',rm);
                            window.localStorage.setItem('report-list-22', reportTitle);
                            window.setTimeout(function() {
                                window.top.location.hash="";
                                window.location.href = "login.html";
                                Kit.loading.hide('html');
                            }, 100);
                        } else {
                            Kit.alert2(res.msg);
                        }
                        Kit.loading.hide(loadingArea);
                    });
                });
            }
        };
        return Profile;
    });
